package com.javafactroy.kunlong.bip.icp.message.test.netty.handler.client;


import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.DefaultPromise;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.javafactroy.kunlong.bip.icp.message.base.iface.IMessage;
import com.javafactroy.kunlong.bip.icp.message.test.netty.codec.TMsgByte2MsgDecoder;
import com.javafactroy.kunlong.bip.icp.message.test.netty.codec.TMsgClientLengthFieldDecoder;
import com.javafactroy.kunlong.bip.icp.message.test.netty.codec.TMsgMsg2ByteEncoder;


@Component
@Data
public class TMsgClientHandlerInitilizer extends ChannelInitializer<Channel> {

    /**
     * @Fields clientHandler : 客户端处理
     */
    @Autowired
    private TMsgClientHandler clientHandler;
    public void setPromise(DefaultPromise<IMessage> promise){
        getClientHandler().setPromise(promise);

    }

    public DefaultPromise<IMessage> getPromise (){
        return getClientHandler().getPromise() ;

    }
    @Override
    protected void initChannel(Channel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
        if (pipeline.get(TMsgClientLengthFieldDecoder.class) == null) {
            pipeline.addLast(
                    new TMsgClientLengthFieldDecoder(),
                    new TMsgByte2MsgDecoder(),
                    new TMsgMsg2ByteEncoder()).addLast(clientHandler);
        }
    }

}
